
package com.logistics.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.easybuild.core.exceptions.SystemException;

public class DateUtils {
	
	/**
	 * 格式化时间yyyy-MM-dd
	 */
	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 格式化时间yyyy-MM-dd HH:mm:ss
	 */
	private static final SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 年月日(无符号) yyyyMMdd
	 */
	private static final SimpleDateFormat unsignedDateFormat = new SimpleDateFormat("yyyyMMdd");
	
	/**
	 * 年月日时分秒毫秒(无符号) yyyyMMddHHmmss
	 */
	private static final SimpleDateFormat simpleDateTimeMillisecondFormat = new SimpleDateFormat("yyyyMMddHHmmssssss");
	
	/**
	 * 格式化时间yyyy-MM-dd hh:mm:ss.ssss
	 */
	private static final SimpleDateFormat dateTimeMillisecondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.ssss");
    
	private static Calendar calendar = Calendar.getInstance();
	
	/**
	 * 把 yyyy-MM-dd 格式化时间
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date parseDate(String dateStr) {
		try {
			return simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			throw new SystemException(e);
		}
	}
	
	/**
	 * 把 yyyy-MM-dd 格式化时间00:00:00
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateStratTime(String dateStr) {
		try {
			return simpleDateTimeFormat.parse(dateStr + " 00:00:00");
		} catch (ParseException e) {
			throw new SystemException(e);
		}
	}
	/**
	 * 把 yyyy-MM-dd 格式化时间23:59:59
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateEndTime(String dateStr) {
		try {
			return simpleDateTimeFormat.parse(dateStr + " 23:59:59");
		} catch (ParseException e) {
			throw new SystemException(e);
		}
	}
	
	/**
	 * 把 时间格式化yyyy-MM-dd 
	 *
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		return simpleDateFormat.format(date);
	}
	
	/**
	 * 把 yyyy-MM-dd hh:mm:ss 格式化时间
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateTime(String dateStr) {
		try {
			return simpleDateTimeFormat.parse(dateStr);
		} catch (ParseException e) {
			throw new SystemException(e);
		}
	}
	
	/**
	 * 把 时间格式化yyyy-MM-dd 
	 *
	 * @param date
	 * @return
	 */
	public static String formatDateTime(Date date) {
		return simpleDateTimeFormat.format(date);
	}
	
	/**
	 * 把 yyyy-MM-dd HH:mm:ss.ssss 格式化时间
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateTimeMillisecond(String dateStr) {
		try {
			return dateTimeMillisecondFormat.parse(dateStr);
		} catch (ParseException e) {
			throw new SystemException(e);
		}
	}
	
	/**
	 * 把 时间格式化yyyyMMdd
	 *
	 * @param date
	 * @return
	 */
	public static String formatUnsignedDate (Date date) {
		return unsignedDateFormat.format(date);
	}
	
	/**
	 * 把 时间格式化yyyyMMddHHmmssssss 
	 *
	 * @param date
	 * @return
	 */
	public static String formatDateTimeMillisecond (Date date) {
		return simpleDateTimeMillisecondFormat.format(date);
	}
	
	public static Date getThreeMonthBeforeDate() {
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -3);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date getHalfYearBeforeDate() {
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -6);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	public static Date getToday() {
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0); 
		calendar.set(Calendar.MINUTE, 0); 
		calendar.set(Calendar.SECOND, 0); 
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	public static Date getLastWeek() {
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -7);
		calendar.set(Calendar.HOUR_OF_DAY, 0); 
		calendar.set(Calendar.MINUTE, 0); 
		calendar.set(Calendar.SECOND, 0); 
		calendar.set(Calendar.MILLISECOND, 0);
		
		return calendar.getTime();
	}
}
